package com.hz.ruoyifarmbase.baseinfo.controller;

import com.hz.ruoyifarmbase.baseinfo.domain.AgrBasePlots;
import com.hz.ruoyifarmbase.baseinfo.service.IAgrBasePlotsService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地块Controller
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/plots")
public class AgrBasePlotsController extends BaseController
{
    @Autowired
    private IAgrBasePlotsService agrBasePlotsService;

    /**
     * 查询地块列表
     */
    @RequiresPermissions("baseinfo:plots:list")
    @GetMapping("/list")
    public TableDataInfo list(AgrBasePlots agrBasePlots)
    {
        startPage();
        List<AgrBasePlots> list = agrBasePlotsService.selectAgrBasePlotsList(agrBasePlots);
        return getDataTable(list);
    }

    /**
     * 导出地块列表
     */
    @RequiresPermissions("baseinfo:plots:export")
    @Log(title = "地块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgrBasePlots agrBasePlots)
    {
        List<AgrBasePlots> list = agrBasePlotsService.selectAgrBasePlotsList(agrBasePlots);
        ExcelUtil<AgrBasePlots> util = new ExcelUtil<AgrBasePlots>(AgrBasePlots.class);
        util.exportExcel(response, list, "地块数据");
    }

    /**
     * 获取地块详细信息
     */
    @RequiresPermissions("baseinfo:plots:query")
    @GetMapping(value = "/{plotsId}")
    public AjaxResult getInfo(@PathVariable("plotsId") Long plotsId)
    {
        return success(agrBasePlotsService.selectAgrBasePlotsByPlotsId(plotsId));
    }

    /**
     * 新增地块
     */
    @RequiresPermissions("baseinfo:plots:add")
    @Log(title = "地块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgrBasePlots agrBasePlots)
    {
        return toAjax(agrBasePlotsService.insertAgrBasePlots(agrBasePlots));
    }

    /**
     * 修改地块
     */
    @RequiresPermissions("baseinfo:plots:edit")
    @Log(title = "地块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgrBasePlots agrBasePlots)
    {
        return toAjax(agrBasePlotsService.updateAgrBasePlots(agrBasePlots));
    }

    /**
     * 删除地块
     */
    @RequiresPermissions("baseinfo:plots:remove")
    @Log(title = "地块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{plotsIds}")
    public AjaxResult remove(@PathVariable Long[] plotsIds)
    {
        return toAjax(agrBasePlotsService.deleteAgrBasePlotsByPlotsIds(plotsIds));
    }
}
